import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextArea;


/**
 * Dialog that allows Doctors to record Doctor's orders for a patient
 * @author Vishnu
 *
 */
public class DoctorsOrderDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	protected String prescription;
	private JCheckBox chckbxBloodTests;
	private JCheckBox chckbxUrineTests;
	private JCheckBox chckbxImmunizaations;
	protected boolean bloodTests;
	protected boolean urineTests;
	protected boolean immuno;
	protected String instructions;
	private Connection con;
	private JTextArea txtInstructions;
	private JTextArea txtPrescription;


	/**
	 * Constructor for the dialog
	 * @param mac MedAppController mac
	 * @param SSN int SSN of the patient
	 * @param drSSN int drSSN of the current Doctor
	 */
	public DoctorsOrderDialog(final MedAppController mac, final int SSN, final int drSSN) {
		con = mac.getCon();
		setResizable(false);
		setBounds(100, 100, 713, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		JLabel lblPrescription = new JLabel("Prescription");
		lblPrescription.setHorizontalAlignment(SwingConstants.CENTER);
		lblPrescription.setBounds(10, 11, 181, 14);
		contentPanel.add(lblPrescription);
		
		JLabel lblTests = new JLabel("Tests/Immunizations");
		lblTests.setHorizontalAlignment(SwingConstants.CENTER);
		lblTests.setBounds(201, 11, 181, 14);
		contentPanel.add(lblTests);
		
		chckbxBloodTests = new JCheckBox("Blood Tests");
		chckbxBloodTests.addActionListener(new ActionListener() {
			/**
			 * changes the state of the bloodTests variable based on the checkbox state
			 */
			public void actionPerformed(ActionEvent arg0) {
				JCheckBox target = (JCheckBox)arg0.getSource();
				bloodTests = target.isSelected();
			}
		});
		chckbxBloodTests.setBounds(211, 42, 97, 23);
		contentPanel.add(chckbxBloodTests);
		
		chckbxUrineTests = new JCheckBox("Urine Tests");
		chckbxUrineTests.addActionListener(new ActionListener() {
			/**
			 * changes the state of the urineTests variable based on the checkbox state
			 */
			public void actionPerformed(ActionEvent arg0) {
				JCheckBox target = (JCheckBox)arg0.getSource();
				urineTests = target.isSelected();
			}
		});
		chckbxUrineTests.setBounds(211, 107, 97, 23);
		contentPanel.add(chckbxUrineTests);
		
		chckbxImmunizaations = new JCheckBox("Immunizations");
		chckbxImmunizaations.addActionListener(new ActionListener() {
			/**
			 * changes the state of the immunizations variable based on the checkbox state
			 */
			public void actionPerformed(ActionEvent arg0) {
				JCheckBox target = (JCheckBox)arg0.getSource();
				immuno = target.isSelected();
			}
		});
		chckbxImmunizaations.setBounds(211, 172, 120, 23);
		contentPanel.add(chckbxImmunizaations);
		
		JLabel lblInstructionsForPatient = new JLabel("Instructions for Patient");
		lblInstructionsForPatient.setHorizontalAlignment(SwingConstants.CENTER);
		lblInstructionsForPatient.setBounds(461, 11, 181, 14);
		contentPanel.add(lblInstructionsForPatient);
		
		txtPrescription = new JTextArea();
		txtPrescription.setWrapStyleWord(true);
		txtPrescription.setLineWrap(true);
		txtPrescription.setBounds(20, 36, 181, 192);
		contentPanel.add(txtPrescription);
		
		txtInstructions = new JTextArea();
		txtInstructions.setWrapStyleWord(true);
		txtInstructions.setLineWrap(true);
		txtInstructions.setBounds(417, 41, 280, 187);
		contentPanel.add(txtInstructions);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					private int bloodTestValue;
					private int urineTestValue;
					private int immunoValue;

					/**
					 * performs the action for submitting the doctors order
					 */
					public void actionPerformed(ActionEvent arg0) {
						prescription = txtPrescription.getText();
						instructions = txtInstructions.getText();
						bloodTestValue = 0;
						urineTestValue = 0;
						immunoValue = 0;
						
						if(bloodTests)
							bloodTestValue = 1;
						
						if(urineTests){
							urineTestValue = 1;
						}
						
						if(immuno)
							immunoValue = 1;
						String sql = "INSERT INTO `velociraptorsattack`.`doctorsorders` (`PRESCRIPTION`, `BLOOD_TESTS`, `URINE_TESTS`, `IMMUNIZATIONS`, `INSTRUCTIONS`, `DO_FORPATIENTSSN`, `placed_DOCTORSSN`, `DoctorsOrders_ID`) VALUES ('" + prescription + "', '" + bloodTestValue + "', '" + urineTestValue + "', '" + immunoValue + "', '" + instructions + "', '" + SSN + "','" + drSSN + "','"+mac.getNextDoctorsOrderID()+"');";
						mac.out.println(sql);
						
						try {
							Statement stmt = con.createStatement();
							int statement = stmt.executeUpdate(sql);
							JOptionPane.showMessageDialog(null, "Doctor's Orders Added!");
						} catch (SQLException e) {
							// TODO Auto-generated catch block
							JOptionPane.showMessageDialog(null, "Doctor's Orders Invalid!");
							e.printStackTrace();
						}
						
						
						dispose();
						
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					/**
					 * performs the close operation of the cancel button
					 */
					public void actionPerformed(ActionEvent arg0) {
						
						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}
}
